import {
	defineStore
} from 'pinia';
import {
	toGetNoticeList,
	toGetNoticeDetail

} from "@/utils/request/notice.js"
import moment from "moment"
export const useNoticeStore = defineStore('notice', {
	state: () => {
		return {
			msg: {
				code: "",
				message: ""
			},
			noticeList: [],
			noticeDetail: {}

		};
	},
	// 也可以这样定义
	// state: () => ({ count: 0 })
	actions: {
		//获取学校公告
		async getNoticeList(schoolId) {
			try {
				this.noticeList = []
				const res = await toGetNoticeList(schoolId);
				this.msg.code = res.code;
				this.msg.message = res.message;
				if (res.code == "200") {

					res.data.forEach((p) => {
						p.time = moment(p.time).format("YYYY-MM-DD HH:mm:ss")
						this.noticeList.unshift(p)
					})
					this.noticeList = res.data;
				}
			} catch (err) {
				this.msg.code = 405;
				this.msg.message = "网络不好，请稍后重试"
			}
		},
		//获取公告的详情
		async toGetNoticeDetail(noticeId) {
			try {
				const res = await toGetNoticeDetail(noticeId);
				this.msg.code = res.code;
				this.msg.message = res.message;
				if (res.code == "200") {
					res.data.time = moment(res.data.time).format("YYYY-MM-DD HH:mm:ss")
					this.noticeDetail = res.data;
				}
			} catch (err) {
				this.msg.code = 405,
					this.msg.message = "网络不好，请稍后重试"
			}
		},
	}
});